{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "cma2IKnL9e-M"
   },
   "source": [
    "# Exceptions \n",
    "\n",
    "* Programmer Errors \n",
    "* Program Bugs \n",
    "* Exceptions\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 132
    },
    "colab_type": "code",
    "id": "1YGSrszE9cch",
    "outputId": "30e93b4a-a466-4cd9-a46d-1698dece3e55"
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "Missing parentheses in call to 'print'. Did you mean print(\"Hello World!\")? (<ipython-input-2-c7f6c99ffc2b>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-2-c7f6c99ffc2b>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    print \"Hello World!\"\u001b[0m\n\u001b[1;37m                       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(\"Hello World!\")?\n"
     ]
    }
   ],
   "source": [
    "# error example,SyntaxError.\n",
    "\n",
    "print \"Hello World!\"   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "OMnUYXwD_msy"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the first integer: 6\n",
      "Enter the second integer: 7\n",
      "6 + 7 = 67\n"
     ]
    }
   ],
   "source": [
    "#bug example.\n",
    "\n",
    "num1 = input(\"Enter the first integer: \")\n",
    "num2 = input(\"Enter the second integer: \")\n",
    "\n",
    "print(num1, \"+\", num2, \"=\", num1 + num2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "n0Ux_gF9CthH"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First integer: 6\n",
      "Second integer: 7.4\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: '7.4'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-2ca720ac0e5e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mnum3\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"First integer: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mnum4\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Second integer: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"/\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnum4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"=\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnum3\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mnum4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: '7.4'"
     ]
    }
   ],
   "source": [
    "#exception example, ValueError.\n",
    "\n",
    "num3 = int(input(\"First integer: \"))\n",
    "num4 = int(input(\"Second integer: \"))\n",
    "\n",
    "print(num3, \"/\",num4, \"=\", num3/num4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Second integer: 7.4\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: '7.4'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-15b360894f7d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnum4\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Second integer: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mnum4\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: '7.4'"
     ]
    }
   ],
   "source": [
    "num4 = int(input(\"Second integer: \"))\n",
    "num4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(3.8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "D9_dw3TqA0wc"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First integer: 7\n",
      "Second integer: 0\n"
     ]
    },
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-11-c273cdf866df>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mnum4\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Second integer: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"/\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnum4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"=\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnum3\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mnum4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "# ZeroDivisionError.\n",
    "\n",
    "\n",
    "num3 = int(input(\"First integer: \"))\n",
    "num4 = int(input(\"Second integer: \"))\n",
    "\n",
    "print(num3, \"/\",num4, \"=\", num3/num4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "bz2U4A64BnWJ"
   },
   "source": [
    "## Exception Handling\n",
    "\n",
    "\n",
    "try:\n",
    "\n",
    "\n",
    "> the situations where we can get exceptions\n",
    "\n",
    "except \"Exception Name\":\n",
    "\n",
    "\n",
    "> the operations in case of exceptions\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "-2Dkw2H4BpCX",
    "outputId": "f81b0a41-db10-4a9d-852e-00eaf498e0d4"
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'Alan Turing'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-12-417db27f4b47>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Alan Turing\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'Alan Turing'"
     ]
    }
   ],
   "source": [
    "x = \"Alan Turing\"\n",
    "\n",
    "int(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "rAhmuTE9B8cy"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please enter an integer value!!!\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "  int(x)\n",
    "\n",
    "except ValueError:\n",
    "  print(\"Please enter an integer value!!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "E4QMEl4YC5cR"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First integer: 4\n",
      "Second integer: 0\n"
     ]
    },
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-19-194a4a382dd0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      6\u001b[0m   \u001b[0mnum4_int\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m   \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum3_int\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"/\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnum4_int\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"=\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnum3_int\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mnum4_int\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "num3 = input(\"First integer: \")\n",
    "num4 = input(\"Second integer: \")\n",
    "\n",
    "try:\n",
    "  num3_int = int(num3)\n",
    "  num4_int = int(num4)\n",
    "\n",
    "  print(num3_int, \"/\",num4_int, \"=\", num3_int/num4_int)\n",
    "\n",
    "except ValueError:\n",
    "  print(\"Please enter an integer value!!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "ttj6eITbCYik"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First integer: 4\n",
      "Second integer: 0\n",
      "Please enter the second input different than 0 value!!!\n",
      "Enter a integer number3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num3 = input(\"First integer: \")\n",
    "num4 = input(\"Second integer: \")\n",
    "\n",
    "try:\n",
    "  num3_int = int(num3)\n",
    "  num4_int = int(num4)\n",
    "\n",
    "  print(num3_int, \"/\",num4_int, \"=\", num3_int/num4_int)\n",
    "\n",
    "except ZeroDivisionError:\n",
    "    print(\"Please enter the second input different than 0 value!!!\")\n",
    "    ebru = int(input(\"Enter a integer number\"))\n",
    "ebru"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "qgTSl8xJFrnP"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First integer: 0\n",
      "Second integer: 0\n",
      "Please enter the second input different than 0 value!!!\n"
     ]
    }
   ],
   "source": [
    "num3 = input(\"First integer: \")\n",
    "num4 = input(\"Second integer: \")\n",
    "\n",
    "try:\n",
    "  num3_int = int(num3)\n",
    "  num4_int = int(num4)\n",
    "\n",
    "  print(num3_int, \"/\",num4_int, \"=\", num3_int/num4_int)\n",
    "\n",
    "except ValueError:\n",
    "  print(\"Please enter an integer value!!!\")\n",
    "except ZeroDivisionError:\n",
    "  print(\"Please enter the second input different than 0 value!!!\")\n",
    "except:\n",
    "  print(\"Unknown error...\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "ZeroDivisionError",
     "evalue": "float division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-10-71f13771214b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;36m3.4\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m: float division by zero"
     ]
    }
   ],
   "source": [
    "3.4/0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "bGy694JBF4nv"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First integer: 2.5\n",
      "Second integer: 3\n",
      "Error!!!\n"
     ]
    }
   ],
   "source": [
    "num3 = input(\"First integer: \")\n",
    "num4 = input(\"Second integer: \")\n",
    "\n",
    "try:\n",
    "  num3_int = int(num3)\n",
    "  num4_int = int(num4)\n",
    "\n",
    "  print(num3_int, \"/\",num4_int, \"=\", num3_int/num4_int)\n",
    "\n",
    "except (ValueError, ZeroDivisionError):\n",
    "  print(\"Error!!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: '3.5'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-14-f6c874437bf0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"3.5\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: '3.5'"
     ]
    }
   ],
   "source": [
    "int(\"3.5\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "tu1cCIfjJiFM"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First integer: 5\n",
      "Second integer: 5.2\n",
      "Error!!!\n",
      "Error message:  invalid literal for int() with base 10: '5.2'\n"
     ]
    }
   ],
   "source": [
    "#try/except/as\n",
    "\n",
    "\n",
    "num3 = input(\"First integer: \")\n",
    "num4 = input(\"Second integer: \")\n",
    "\n",
    "try:\n",
    "  num3_int = int(num3)\n",
    "  num4_int = int(num4)\n",
    "\n",
    "  print(num3_int, \"/\",num4_int, \"=\", num3_int/num4_int)\n",
    "\n",
    "except ValueError as error:\n",
    "  print(\"Error!!!\")\n",
    "  print(\"Error message: \", error)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "g07-ysSCGQvw"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First number: (Press q for quit the program): 3\n",
      "Second number: 0\n",
      "Error!\n",
      "Please try again!\n",
      "First number: (Press q for quit the program): 3\n",
      "Second number: 3.2\n",
      "Error!\n",
      "Please try again!\n",
      "First number: (Press q for quit the program): 0\n",
      "Second number: 2\n",
      "0 / 2 = 0.0\n",
      "First number: (Press q for quit the program): q\n"
     ]
    }
   ],
   "source": [
    "#exception handling in loop structure\n",
    "\n",
    "\n",
    "while True:\n",
    "    num1 = input(\"First number: (Press q for quit the program): \")\n",
    "\n",
    "    if num1 == \"q\":\n",
    "        break\n",
    "\n",
    "    num2 = input(\"Second number: \")\n",
    "\n",
    "    try:\n",
    "        num1_int = int(num1)\n",
    "        num2_int = int(num2)\n",
    "        print(num1_int, \"/\", num2_int, \"=\", num1_int / num2_int)\n",
    "    except (ValueError, ZeroDivisionError):\n",
    "        print(\"Error!\")\n",
    "        print(\"Please try again!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First integer: 5\n",
      "Second integer: 0\n",
      "Error!!!\n",
      "Error message:  division by zero\n"
     ]
    }
   ],
   "source": [
    "#try/except/as\n",
    "\n",
    "\n",
    "num3 = input(\"First integer: \")\n",
    "num4 = input(\"Second integer: \")\n",
    "\n",
    "try:\n",
    "  num3_int = int(num3)\n",
    "  num4_int = int(num4)\n",
    "\n",
    "  print(num3_int, \"/\",num4_int, \"=\", num3_int/num4_int)\n",
    "\n",
    "except (ValueError, ZeroDivisionError) as error:\n",
    "  print(\"Error!!!\")\n",
    "  print(\"Error message: \", error)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "S7czCxD3KTKx"
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "exception handling in functions\n",
    "using raise command\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "def reverse(s):\n",
    "\n",
    "    if (type(s) != str):\n",
    "        raise ValueError(\"Please enter a String type.\")\n",
    "    else:\n",
    "        return s[::-1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 36
    },
    "colab_type": "code",
    "id": "2t0fmpb7MOfp",
    "outputId": "676c7296-d80c-4b98-8fc2-1851888d1699"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'nohtyp'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reverse(\"python\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Qp4QI6GhzBgC"
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Please enter a String type.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-32-9be0aeb641b6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mreverse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m12\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-30-421a260d7203>\u001b[0m in \u001b[0;36mreverse\u001b[1;34m(s)\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Please enter a String type.\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     11\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Please enter a String type."
     ]
    }
   ],
   "source": [
    "reverse(12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "H6k8jXA_OKDH"
   },
   "source": [
    "# Intro to Object Oriented Programming\n",
    "\n",
    "Every entity we see around us is an object.\n",
    "Each object has its own attributes and a set of functions (methods) they perform.\n",
    "\n",
    "The concept we call class is also the data types that enable us to generate objects.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Car():\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello():\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "RvIjixb-ONiw"
   },
   "outputs": [],
   "source": [
    "class Car():\n",
    "    colour = \"yellow\"\n",
    "    brand = \"xyz\"\n",
    "    door = 4\n",
    "    model = 1987\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "PHXc3gzoZVRK",
    "outputId": "be0b6557-da92-46cc-8241-1bb3137466fb"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<__main__.Car object at 0x000002797141FD88>\n"
     ]
    }
   ],
   "source": [
    "car1 = Car()\n",
    "\n",
    "print(car1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "8Z38odyhZj2x"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1987\n",
      "xyz\n",
      "4\n",
      "yellow\n"
     ]
    }
   ],
   "source": [
    "print(car1.model)\n",
    "print(car1.brand)\n",
    "print(car1.door)\n",
    "print(car1.colour)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "ASMwsKZQZ73S"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'yellow'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car2 = Car()\n",
    "\n",
    "car2.colour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "gvURL79zaMSZ"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1987"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#We can also call attributes over the class without creating an object.\n",
    "\n",
    "Car.model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 36
    },
    "colab_type": "code",
    "id": "D-bSYoMcaRbD",
    "outputId": "2193f663-a22d-434c-9ff0-c2d7d39765bd"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'xyz'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Car.brand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "ODInfukGbGLo"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__class__',\n",
       " '__delattr__',\n",
       " '__dict__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__le__',\n",
       " '__lt__',\n",
       " '__module__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " '__weakref__',\n",
       " 'brand',\n",
       " 'colour',\n",
       " 'door',\n",
       " 'model']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(car1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "iB2P1I88aZTB"
   },
   "source": [
    "### __init()__\n",
    "\n",
    "In order to create each object with different values at the beginning,\n",
    "we need to send the values of the objects.\n",
    "For this, we use a special method: __init () __\n",
    "\n",
    "The init () method is defined as a constructor function.\n",
    "\n",
    "It is the first function that is automatically called up while creating our objects.\n",
    "\n",
    "By defining this method specially, we can create our objects with different values.\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "VdPxg1DUbrRE"
   },
   "outputs": [],
   "source": [
    "class Car2():\n",
    "\n",
    "  def __init__(self): \n",
    "    print(\"init function calling...\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "rukOVNMxc3_T"
   },
   "source": [
    "\n",
    "self= to access any variable or other methods in the defined class.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "BVfHG7DXcdGj",
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init function calling...\n"
     ]
    }
   ],
   "source": [
    "car = Car2()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "HSQQs2f0eZpr"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "red\n",
      "xyz\n"
     ]
    }
   ],
   "source": [
    "class Car3():\n",
    "\n",
    "  def __init__(self, colour, brand, door):\n",
    "    self.colour = colour\n",
    "    self.brand = brand\n",
    "    self.door = door\n",
    "\n",
    "\n",
    "red_car = Car3(\"red\", \"abc\",2)\n",
    "\n",
    "yellow_car = Car3(\"yellow\", \"xyz\", 4)\n",
    "\n",
    "print(red_car.colour)\n",
    "print(yellow_car.brand)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Awm6VrLEObqE"
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "__init__() missing 3 required positional arguments: 'colour', 'brand', and 'door'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-55-2993678ac391>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mblue_car\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mCar3\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: __init__() missing 3 required positional arguments: 'colour', 'brand', and 'door'"
     ]
    }
   ],
   "source": [
    "blue_car = Car3()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "__init__() missing 1 required positional argument: 'door'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-60-6b3d9a135cf0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mblue_car\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mCar3\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"blue\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"xyz\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: __init__() missing 1 required positional argument: 'door'"
     ]
    }
   ],
   "source": [
    "blue_car = Car3(\"blue\",\"xyz\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bnm\n",
      "blue\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "blue_car = Car3(\"blue\",\"bnm\",10)\n",
    "\n",
    "print(blue_car.brand)\n",
    "print(blue_car.colour)\n",
    "print(blue_car.door)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "YjSEGRYrfQ69"
   },
   "outputs": [],
   "source": [
    "class Person():\n",
    "  name = \"\"\n",
    "  age = 0\n",
    "\n",
    "  def __init__(self, personName, personAge):\n",
    "    self.name = personName\n",
    "    self.age = personAge\n",
    "\n",
    "\n",
    "  def welcomePerson(self):\n",
    "    print(\"Hello \" + self.name)\n",
    "\n",
    "  \n",
    "  def showAge(self):\n",
    "    print(self.age)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "GeZqS15XhmM7"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Asli\n"
     ]
    }
   ],
   "source": [
    "person1 = Person (\"Asli\", 28)\n",
    "\n",
    "person1.welcomePerson()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "imIm2qrdjESE"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28\n"
     ]
    }
   ],
   "source": [
    "person1.showAge()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Person3():\n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.language = []\n",
    "        \n",
    "    def welcomePerson(self):\n",
    "        print(\"Hello\",self.name)\n",
    "    \n",
    "    def showAge(self):\n",
    "        print(self.age)\n",
    "        \n",
    "    def addLanguage(self,new_lang):\n",
    "        print(\"Adding new language\")\n",
    "        self.language.append(new_lang)\n",
    "    def showInfo(self):\n",
    "        print(\"{} is {} years old\".format(self.name,self.age))\n",
    "        print(\"He can speak:\")\n",
    "        for i in self.language:\n",
    "              print(i)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "Ferhat = Person3(\"Ferhat\",21) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.Person3 at 0x27972501548>"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Ferhat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding new language\n"
     ]
    }
   ],
   "source": [
    "Ferhat.addLanguage(\"Spanish\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21\n"
     ]
    }
   ],
   "source": [
    "Ferhat.showAge()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ferhat is 21 years old\n",
      "He can speak:\n",
      "Spanish\n"
     ]
    }
   ],
   "source": [
    "Ferhat.showInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Ferhat\n"
     ]
    }
   ],
   "source": [
    "Ferhat.welcomePerson()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding new language\n"
     ]
    }
   ],
   "source": [
    "Ferhat.addLanguage(\"Spanish\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ferhat is 21 years old\n",
      "He can speak:\n",
      "Spanish\n",
      "Spanish\n"
     ]
    }
   ],
   "source": [
    "Ferhat.showInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding new language\n"
     ]
    }
   ],
   "source": [
    "Ferhat.addLanguage(\"English\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ferhat is 21 years old\n",
      "He can speak:\n",
      "Spanish\n",
      "Spanish\n",
      "English\n"
     ]
    }
   ],
   "source": [
    "Ferhat.showInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "Day_6.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
